﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Pong
{
    class Ball
    {
        Texture2D texture;
        public Vector2 position;
        Rectangle size;
        Vector2 velocity;
        Random random = new Random();
        SoundEffect soundeffect;
        
        public Ball(GameWindow janela , Texture2D texture, Vector2 position,SoundEffect soundeffect)//<CONSTRUTOR!!!
        {
            this.texture = texture;
            this.size = texture.Bounds;

            if (position.X == 0 && position.Y == 0) // se não for informada posição, a bola será criada no centro da tela
            {
                this.position = new Vector2(
                janela.ClientBounds.Width / 2
                - this.size.Width / 2,
                janela.ClientBounds.Height / 2
                - this.size.Height / 2);
                
            }
            else // criada na posição informada
            {
                this.position = position;
            }

            
            float posicaoX = random.Next(-5,5);
            float posicaoY = random.Next(-5,5);

            

            this.velocity = new Vector2(posicaoX,posicaoY);

            this.soundeffect = soundeffect;
            
        }

        public void Update (GameTime gameTime, GameWindow janela, Pallete p1, Pallete p2)
        {

           ManternaTela(janela,p1, p2);

           
           position  += velocity; //atualiza posição da bolinha
           
           this.size.X = (int)position.X;
           this.size.Y = (int)position.Y;
            
           if (p1.size.Intersects(size))
           {
                velocity.X *= -1;
           }

           if (p2.size.Intersects(size))
           {
               velocity.X *= -1;
           }
        }

        public void ManternaTela(GameWindow janela, Pallete p1, Pallete p2)
        {
            if (position.Y <= 0)
            {
                position.Y = 0;
                velocity.Y *= -1;
                soundeffect.Play();
            }

            if (position.Y >= janela.ClientBounds.Height - size.Height)
            {
                position.Y = janela.ClientBounds.Height - size.Height;
                velocity.Y *= -1;
                soundeffect.Play();
            }

            if (position.X <= 0)
            {
                position.X = 0;
                velocity.X *= -1;
                soundeffect.Play();
                p1.points += 1;
            }

            if (position.X >= janela.ClientBounds.Width - size.Width)
            {
                position.X = janela.ClientBounds.Width - size.Width;
                velocity.X *= -1;
                soundeffect.Play();
                p2.points += 1;
            }
        }


        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(texture, position, Color.White);

        }


        
    }

}
